<?php
$params = $_POST;
$storeFile = 'tickets.json';
$json = [];
if (file_exists($storeFile)) {
    $json = json_decode(file_get_contents($storeFile), true);
}

header('Content-Type: application/json');

try {
    $json[$params['key']] = $params['user'];
    file_put_contents($storeFile, json_encode($json));
    
    // check
    $res = json_decode(file_get_contents($storeFile), true);
    if (isset($res[$params['key']]) && $res[$params['key']] == $params['user']) {
        echo json_encode(['status' => 'success']);
    } else {
        echo json_encode(['status' => 'fail']);
    }
} catch (Exception $e) {
    echo json_encode(['status' => 'fail', 'message' => $e->getMessage()]);
}